package wb.is.besti.edu.cd.javalibgame;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/12/18.
 */

public class AnimaLayerAdd extends FrameLayout {
    private List<AddCard> addCards = new ArrayList<AddCard>();
    public AnimaLayerAdd(@NonNull Context context) {
        super(context);
    }

    public AnimaLayerAdd(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AnimaLayerAdd(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private AddCard getCard(int num){
        AddCard card;
        if (addCards.size()>0){
            card = addCards.remove(0);
        }
        else {
            card = new AddCard(getContext());
            addView(card);
        }
        card.setVisibility(View.VISIBLE);
        card.setNum(num);
        return card;
    }
    private void recycleCard(AddCard c) {
        c.setVisibility(View.INVISIBLE);
        c.setAnimation(null);
        addCards.add(c);
    }
    public void createScaleTo1(AddCard target) {
        ScaleAnimation sa = new ScaleAnimation(0.1f, 1, 0.1f, 1,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        sa.setDuration(100);
        target.setAnimation(null);
        target.getTextView().startAnimation(sa);
    }
    public void createMoveAnim(final AddCard from, final AddCard to, int fromX,
                               int toX, int fromY, int toY) {

        final AddCard c = getCard(from.getNum());
        LayoutParams lp = new LayoutParams(Config.CARD_WIDTH, Config.CARD_WIDTH);
        lp.leftMargin = fromX * Config.CARD_WIDTH;
        lp.topMargin = fromY * Config.CARD_WIDTH;
        c.setLayoutParams(lp);

        if (to.getNum() <= 0) {
            to.getTextView().setVisibility(View.INVISIBLE);
        }


        TranslateAnimation ta = new TranslateAnimation(0, Config.CARD_WIDTH * (toX - fromX), 0, Config.CARD_WIDTH * (toY - fromY));
        ta.setDuration(100);
        ta.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                to.getTextView().setVisibility(View.VISIBLE);
                recycleCard(c);
            }
        });
        c.startAnimation(ta);
    }
}
